package cn.golxen.rsms.redis;


import cn.golxen.rsms.mapper.GoodsMapper;
import cn.golxen.rsms.service.GoodsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.listener.KeyExpirationEventMessageListener;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.stereotype.Component;

import java.util.Objects;
import java.util.concurrent.TimeUnit;

@Slf4j
@Component
public class RedisExpiredKeyListener extends KeyExpirationEventMessageListener {

    @Autowired
    RedisTemplate<String,String> redisTemplate;
    @Autowired
    GoodsService goodsService;
    @Autowired
    RedisUtils redisUtils;


    public RedisExpiredKeyListener(RedisMessageListenerContainer listenerContainer) {
        super(listenerContainer);
    }

    @Override
    public void onMessage(Message message, byte[] pattern) {
        log.info("监听到过期key："+message.toString());
        log.info(new String(pattern));
        if (message.toString().contains("timer::suggested_price")){
            //调用计算最多门店使用价格的服务
            goodsService.updateGoodsMainSuggestedPrice(message.toString().split("::")[2]);
            //重新设置计时
            redisUtils.timerSuggestedPrice(message.toString().split("::")[2]);
        }
    }


}
